<template>
	<view>
		<template v-if='loading'>
			<view v-for="(item,index) in 5" :key="index">
				<view class=" Skeleton" style="height: 30rpx;" />
				<view class="activeContent" style="border: none;">
					<view>
						<album :loading='loading' />
					</view>
					<view class=" Skeleton" style="height: 30rpx;margin-top: 20rpx;" />
				</view>
			</view>
		</template>
		<template v-else>
			<view class="activeBox" v-for="(item,index) in list" :key="index">
				<view class="created_at">
					<u-avatar mode='aspectFill' :src='item.user.avatar' size='80rpx' />
					<view style="margin:0 10rpx;flex: 1;">
						<view>
							{{item.user.nick_name}}
						</view>
						<view>
							{{item.last_different_weight>0?`减重${item.last_different_weight}`:`增重${0-item.last_different_weight}`}}Kg
						</view>
					</view>
					<view style="display: flex;align-items: center;">
						<view>
							{{item.total_different_weight}}Kg
						</view>
						<u-icon name='arrow-rightward' />
						<view>
							{{weight}}Kg
						</view>
					</view>
				</view>
				<view class="activeContent">
					<view class="content">
						<view>
							<album :ref='`album${index}`' :imageList='item.images'
								:video='item.videos.map(imageItem=>imageItem.url)' :index='index'
								:playIndex='playIndex' />
						</view>
						<view style="margin: 10rpx 0;">
							{{item.title}}·第{{getTimeDifferenceInDays(start_at,item.created_at)}}天
						</view>
						<view class="created_at">
							<view>
								{{formatFun(item.created_at)}}
							</view>
							<u-icon v-if="user.id==item.user.id" name='more-dot-fill' @click='openSelect(item)' />
						</view>
					</view>
				</view>
			</view>
			<view v-if="!list.length">
				<empty />
			</view>
		</template>
		<root-portal>
			<u-action-sheet :actions="columns" :show="show" @close='show=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
		</root-portal>
	</view>
</template>

<script>
	import {
		format
	} from "timeago.js";
	import album from '@/components/album.vue';
	export default {
		components: {
			album,
		},
		props: ['list', 'loading', 'playIndex', 'weight', 'start_at'],
		data() {
			return {
				d: false,
				selectItem: {},
				show: false,
				user: uni.getStorageSync("user"),
				columns: [{
						name: '编辑'
					},
					{
						name: '删除'
					},
				],
			}
		},
		mounted() {},
		methods: {
			openSelect(item) {
				if (this.d) return
				this.show = true
				this.selectItem = item
			},
			confirm(e) {
				this.selectShow = false
				if (e.name == '删除') {
					this.$emit('ListloadingChange')
					this.d = true
					this.$api.Active.Del(this.selectItem.id).then(res => {
						this.d = false
						uni.$u.toast('删除成功')
						this.$emit('reload')
					}).catch(err => {
						this.$emit('ListloadingChange')
						this.d = false
					})
				} else if (e.name == '编辑') {
					this.$utils.navigateTo({
						url: '/Active/update?id=' + this.selectItem.id
					})
				}
			},
			getTimeDifferenceInDays(timestamp1, timestamp2) {
				// 将时间戳转换为日期对象
				const date1 = new Date(timestamp1);
				const date2 = new Date(timestamp2);

				// 计算两个日期之间的毫秒差
				const timeDifferenceInMilliseconds = Math.abs(date2 - date1);

				// 将毫秒转换为天数，一天有 24 * 60 * 60 * 1000 毫秒
				const oneDayInMilliseconds = 24 * 60 * 60 * 1000;
				const differenceInDays = Math.ceil(timeDifferenceInMilliseconds / oneDayInMilliseconds);

				return differenceInDays - 1;
			},
			formatFun(item) {
				return format(item, "zh_CN")
			},
		}
	}
</script>

<style lang="scss" scoped>
	.created_at {
		font-size: 28rpx;
		display: flex;
		align-items: center;
		margin-right: 40rpx;
		justify-content: space-between;

	}

	.activeContent {
		padding: 0 0 0 35rpx;
		margin: 10rpx 0 10rpx 35rpx;
		border-left: 1rpx dashed #bbb;

		>.content {
			box-shadow: 0 0 10rpx #ddd;
			margin-right: 20rpx;
			padding: 20rpx;
			border-radius: 8rpx;
			background-color: #fff;
		}
	}



	.userBox {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx 0;

		>view:nth-child(2) {
			flex: 0.98;
		}
	}
</style>